package com.fofia.iotweb.web.controllers.admin;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.fofia.iotweb.cache.UserCache;
import com.fofia.iotweb.framework.core.BaseController;
import com.fofia.iotweb.framework.core.RequestHelper;
import com.fofia.iotweb.framework.dto.DictDto;
import com.fofia.iotweb.web.dbdomain.models.admin.SysDictionary;
import com.fofia.iotweb.web.dto.DataTableDto;
import com.fofia.iotweb.web.dto.JsonResult;
import com.fofia.iotweb.web.dto.ResultAdaptDto;
import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.ModelAndView;
import com.fofia.iotweb.web.dbdomain.services.admin.SysDictService;

import java.util.List;

@Mapping("admin/dict")
@Controller
public class DictController extends BaseController {

    @Inject
    private RequestHelper requestHelper;

    @Inject
    private  SysDictService dictService;

    @Inject
    private UserCache userCache;
    @Get
    @Mapping("index")
    @AuthPermissions("admin.dict.view")
    public ModelAndView index(){

        return view();
    }

    @Post
    @Mapping("getPageList")
    @AuthPermissions("admin.dict.view")
    public DataTableDto getPageList(DataTableDto dto){
        String dictName=requestHelper.getString("dictName","");
        String dictCode=requestHelper.getString("dictCode","");

       DataTableDto data= dictService.getPageList(dto,dictName,dictCode,0);
       return data;
    }


    @Get
    @Mapping("form")
    @AuthPermissions("admin.dict.add;amdin.dict.edit")
    public ModelAndView form(){
          String id=requestHelper.getString("id","0");
        return view().put("id",id);
    }


    @Get
    @Mapping("getModel")
    @AuthPermissions("admin.dict.add;amdin.dict.edit")
    public ResultAdaptDto getModel(){
        Long id=requestHelper.getInt64("id",0L);
        SysDictionary model=dictService.getModel(id);
        ResultAdaptDto ret= JsonResult.success();
        ret.put("model",model);
        return ret;
    }

    @Post
    @Mapping("saveData")
    @AuthPermissions("admin.dict.add;amdin.dict.edit")
    public ResultAdaptDto saveData(SysDictionary dto){
        dictService.saveData(dto,userCache.getUserSeesion());
        return JsonResult.success("字典数据已保存");

    }


    @Get
    @Mapping("sonView")
    @AuthPermissions("admin.dict.view")
    public ModelAndView sonView(){

        String pid=requestHelper.getString("pid","0");

        List<DictDto> list=dictService.getDicSelList();
        return view().put("pid",pid).put("list",list);
    }

    @Get
    @Mapping("sonForm")
    @AuthPermissions("admin.dict.add;amdin.dict.edit")
    public ModelAndView sonForm(){
        String id=requestHelper.getString("id","0");
        String pid=requestHelper.getString("pid","0");

        SysDictionary dict=dictService.getModel(Convert.toLong(pid,0L));
        String dictCode="";
        if(dict!=null){
            dictCode=dict.getDicCode();
        }
        List<DictDto> list=dictService.getDicSelList();
        return view().put("id",id).put("pid",pid).put("dictCode",dictCode);
    }
    @Post
    @Mapping("getSonPageList")
    @AuthPermissions("admin.dict.view")
    public DataTableDto getSonPageList(DataTableDto dto){
        Long pid=requestHelper.getInt64("pid",0L);
        String dictName=requestHelper.getString("dictName","");
        String dictCode=requestHelper.getString("dictCode","");

        DataTableDto data= dictService.getPageList(dto,dictName,dictCode,pid);
        return data;
    }

    @Get
    @Mapping("removeAll")
    @AuthPermissions("admin.role.delete")
    public ResultAdaptDto removeAll(){
        String ids=requestHelper.getString("ids","");
        if(StrUtil.isEmpty(ids)){
            return JsonResult.failure("参数错误");
        }
        dictService.removeAll(ids);
        return  JsonResult.success("数据删除成功");
    }

    @Get
    @Mapping("removeSubAll")
    @AuthPermissions("admin.role.delete")
    public ResultAdaptDto removeSubAll(){
        String ids=requestHelper.getString("ids","");
        if(StrUtil.isEmpty(ids)){
            return JsonResult.failure("参数错误");
        }
        dictService.removeSubAll(ids);
        return  JsonResult.success("数据删除成功");
    }
}
